package com.up.management.service.impl;

import com.up.management.domain.entity.EventApplication;
import com.up.management.domain.dto.EventApplicationAuditDTO;
import com.up.management.domain.vo.EventApplicationVO;
import com.up.management.mapper.EventApplicationMapper;
import com.up.management.service.IEventApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.up.management.domain.entity.Event;
import com.up.management.mapper.EventMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;

/**
 * 申请管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-11
 */
@Service
public class EventApplicationServiceImpl implements IEventApplicationService 
{
    @Autowired
    private EventApplicationMapper eventApplicationMapper;

    @Autowired
    private EventMapper eventMapper;

    /**
     * 查询申请管理
     * 
     * @param id 申请管理主键
     * @return 申请管理
     */
    @Override
    public EventApplication selectEventApplicationById(Long id)
    {
        return eventApplicationMapper.selectEventApplicationById(id);
    }

    /**
     * 查询申请管理列表
     * 
     * @param eventApplication 申请管理
     * @return 申请管理
     */
    @Override
    public List<EventApplication> selectEventApplicationList(EventApplication eventApplication)
    {
        return eventApplicationMapper.selectEventApplicationList(eventApplication);
    }

    /** VO 列表（用于前端展示） */
    @Override
    public List<EventApplicationVO> selectEventApplicationVOList(EventApplication eventApplication)
    {
        return eventApplicationMapper.selectEventApplicationVOList(eventApplication);
    }

    /**
     * 新增申请管理
     * 
     * @param eventApplication 申请管理
     * @return 结果
     */
    @Override
    public int insertEventApplication(EventApplication eventApplication)
    {
        return eventApplicationMapper.insertEventApplication(eventApplication);
    }

    /**
     * 修改申请管理
     * 
     * @param eventApplication 申请管理
     * @return 结果
     */
    @Override
    public int updateEventApplication(EventApplication eventApplication)
    {
        return eventApplicationMapper.updateEventApplication(eventApplication);
    }

    /**
     * 审核申请管理：
     * - 更新申请状态与审核备注
     * - 当审核通过（applyStatus == 1）时，将该申请迁移到 ich_event 表
     * 与状态更新置于同一事务，保证一致性
     */
    @Override
    @Transactional
    public int auditEventApplication(EventApplicationAuditDTO dto)
    {
        // 1. 更新申请状态与审核备注
        EventApplication ea = new EventApplication();
        ea.setId(dto.getId());
        if (dto.getApplyStatus() != null) {
            ea.setApplyStatus(dto.getApplyStatus().longValue());
        }
        ea.setReviewRemark(dto.getReviewRemark());
        int result = eventApplicationMapper.auditEventApplication(ea);

        // 2. 审核通过则迁移到活动表
        if (result > 0 && dto.getApplyStatus() != null && dto.getApplyStatus() == 1) {
            EventApplication full = eventApplicationMapper.selectEventApplicationById(dto.getId());
            if (full != null) {
                // 始终按自增主键插入新活动，不绑定申请ID，避免覆盖历史数据
                Event ev = new Event();
                Date now = new Date();
                ev.setName(full.getEventName());
                ev.setCategoryId(full.getCategoryId());
                ev.setRegionId(full.getRegionId());
                ev.setStartTime(full.getStartTime());
                ev.setEndTime(full.getEndTime());
                ev.setLocation(full.getLocation());
                ev.setOrganizer(full.getOrganizer());
                ev.setIntroduction(full.getIntroduction());
                ev.setStatus(1L);
                ev.setCreatedAt(now);
                ev.setUpdatedAt(now);
                ev.setImage(full.getCoverImage());
                // 插入活动，获取数据库生成的自增ID
                eventMapper.insertEvent(ev);
                Long targetEventId = ev.getId();

                // 插入活动详情，关联新生成的活动ID
                eventMapper.insertEventDetail(
                        targetEventId,
                        full.getDetailContent(),
                        full.getDetailImages(),
                        null,
                        now,
                        now
                );
            }
        }
        return result;
    }

    /**
     * 批量删除申请管理
     * 
     * @param ids 需要删除的申请管理主键
     * @return 结果
     */
    @Override
    public int deleteEventApplicationByIds(Long[] ids)
    {
        return eventApplicationMapper.deleteEventApplicationByIds(ids);
    }

    /**
     * 删除申请管理信息
     * 
     * @param id 申请管理主键
     * @return 结果
     */
    @Override
    public int deleteEventApplicationById(Long id)
    {
        return eventApplicationMapper.deleteEventApplicationById(id);
    }
}
